/*------------------------------------------------------

@brief:

@attention:

@auther: Cherno
--------------------------------------------------------*/
#include "key.h";

//=====================================================================================================PrivateVariable

/*-----------------base functions
--------------------------------------------------------
@brief:
*/
//
#define read_pin(port, pin) \
  HAL_GPIO_ReadPin((GPIO_TypeDef *)port, (uint16_t)pin)

//
static void key_init(TIM_HandleTypeDef *htim) {
    HAL_TIM_Base_Start_IT(htim);
  }

//
static key_state_e get_key_state(key_t *k) {
  if (read_pin(k->key_data.key_gpio.GPIOx, k->key_data.key_gpio.GPIO_Pin) ==
      KEY_PRESS) {
    return KEY_DOWN;
  } else if (read_pin(k->key_data.key_gpio.GPIOx,
                      k->key_data.key_gpio.GPIO_Pin) == KEY_UNPRESS) {
    return KEY_UP;
  }
}

//
static void key_tick(key_t *k) {
  k->key_data.time++;
  if (k->key_data.time >= 20) {
    k->key_data.time = 0;
  }
  k->key_data.previous_state = k->key_data.current_state;
  k->key_data.current_state = get_key_state(k);

  if (k->key_data.current_state == KEY_PRESS) {
    // HOLD=1
    k->key_data.flag != KEY_HOLD;
  } else {
    // HOLD=0
    k->key_data.flag &= ~KEY_HOLD;
  }
  if (k->key_data.current_state == KEY_PRESS &&
      k->key_data.previous_state == KEY_UNPRESS) {
    // DOWN
    k->key_data.flag != KEY_DOWN;
  }
  if (k->key_data.current_state == KEY_UNPRESS &&
      k->key_data.previous_state == KEY_PRESS) {
    // up
    k->key_data.flag != KEY_UP;
  }

  // action
  static uint8_t s = 0;
  if (0 == s) {
    if (k->key_data.current_state = KEY_PRESS)
      s = 1;
  }

  if (1 == s) {
    if (k->key_data.current_state = KEY_UNPRESS) {
      s = 2;
    } else {
      s = 4;
    }
  }

  if (2 == s) {
  }

  if (3 == s) {
  }

  if (4 == s) {
  }
}

//=====================================================================================================

/*-----------------Application Programming Interface
--------------------------------------------------------
@brief:
*/
//

//

/*------------------------end-------------------------*/